package com.glacier.cloud.order.config;

import com.glacier.cloud.order.feign.CustomGlobalRequestInterceptor;
import feign.Contract;
import feign.Logger;
import feign.Request;
import feign.auth.BasicAuthRequestInterceptor;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * date 2023-12-16 19:01
 *
 * @author glacier
 * @version 1.0
 */
@EnableFeignClients(basePackages = {"com.glacier.cloud.order.remote"})
@Configuration
public class FeignConfig {

    /**
     * 设置鉴权
     *
     * @return
     */
    @Bean
    BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user", "password");
    }

    /**
     * 设置全局拦截器
     *
     * @return
     */
    @Bean
    CustomGlobalRequestInterceptor customGlobalRequestInterceptor() {
        return new CustomGlobalRequestInterceptor();
    }

    /**
     * 设置日志级别
     *
     * @return
     */
    @Bean
    Logger.Level loggerLevel() {
        return Logger.Level.FULL;
    }

    /**
     * 设置契约注解
     * 开启后，就只能使用 feign 的注解，不能使用springmvc的注解
     *
     * @return
     */
//    @Bean
    Contract contract() {
        return new Contract.Default();
    }


    /**
     * feign 超时时间配置
     * 连接 默认 2 秒
     * 请求处理 默认 5 秒
     *
     * @return
     */
    @Bean
    public Request.Options feignOptions() {
        return new Request.Options(Duration.ofSeconds(2), Duration.ofSeconds(5), true);
    }
}
